package com.zgc.web.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zgc.domain.PageBean;
import com.zgc.domain.SourceItem;
import com.zgc.domain.TeachSource;
import com.zgc.domain.User;
import com.zgc.service.ISourceItemService;
import com.zgc.utils.ZGCContext;
@Controller
@Scope("prototype")
public class SourceItemAction extends ActionSupport implements ModelDriven<SourceItem>{
	private SourceItem sourceItem = new SourceItem();
	@Resource
	private ISourceItemService sourceItemService;
	private List<SourceItem> sourceList = new ArrayList<SourceItem>();
	private int sourceId;
	private PageBean pageBean;
	private int page = 1;
	private String methodName;
	
	
	public String getMethodName() {
		return methodName;
	}
	public void setMethodName(String methodName) {
		this.methodName = methodName;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public PageBean getPageBean() {
		return pageBean;
	}
	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}
	public int getSourceId() {
		return sourceId;
	}
	public void setSourceId(int sourceId) {
		this.sourceId = sourceId;
	}
	public List<SourceItem> getSourceList() {
		return sourceList;
	}
	public void setSourceList(List<SourceItem> sourceList) {
		this.sourceList = sourceList;
	}
	//查询所有资源条目
	public String queryAllItem() {
		Integer uid = ZGCContext.getLoginUser().getUid();
		if (uid==null && uid==1) {
			return null;
		}
		String hql = " from SourceItem where uid = "+uid; //查询语句    
		pageBean = sourceItemService.queryForPage(5, page,hql);
		sourceList = pageBean.getList();
		methodName = "queryAllItem";
		return SUCCESS;
		
	}
	//根据预约状态查询资源条目
	public String queryItemByStatus() {
		int status = Integer.parseInt(ServletActionContext.getRequest().getParameter("status"));
		User loginUser = ZGCContext.getLoginUser();
		Integer uid = loginUser.getUid();
		String hql1 = " from SourceItem where status = "+status;
		String hql2 = " from SourceItem where status = "+status+" and uid = "+uid;
		if (loginUser!=null && loginUser.getAuthority()==1) {
			pageBean = sourceItemService.queryForPage(5, page,hql1);
			sourceList = pageBean.getList();
		}else if (loginUser != null && loginUser.getAuthority()==0) {
			pageBean = sourceItemService.queryForPage(5, page,hql2);
			sourceList = pageBean.getList();
		}else {
			return null;
		}
		methodName = "queryItemByStatus";
		return SUCCESS;
		
	}
	//改变资源条目的状态值
	public String changeStatus() {
		int itemId = Integer.parseInt(ServletActionContext.getRequest().getParameter("itemId"));
		sourceItem  = sourceItemService.qryItemById(itemId);
		Integer status = sourceItem.getStatus();
		if (status==1) {
			sourceItem.setStatus(status-1);
			sourceItemService.update(sourceItem);
		}else{
			sourceItem.setStatus(status+1);
			sourceItemService.update(sourceItem);
		}
		return SUCCESS;
		
	}
	//审批资源
	public String approveStatus() {
		int itemId = Integer.parseInt(ServletActionContext.getRequest().getParameter("itemId"));
		sourceItem  = sourceItemService.qryItemById(itemId);
		Integer status = sourceItem.getStatus();
		sourceItem.setStatus(status+1);
		sourceItemService.update(sourceItem);
		return SUCCESS;
	}
	//插入预约资源条目
	public String insertSourceItem() {
//		int sourceId = Integer.parseInt(ServletActionContext.getRequest().getParameter("sourceId"));
		User loginUser = ZGCContext.getLoginUser();
		Integer uid = loginUser.getUid();
		if (loginUser != null) {
//			sourceItem.setSourceId(sourceId);
			sourceItem.setUid(uid);
			sourceItemService.insertSourceItem(sourceItem);
			return SUCCESS;
		}else {
			return null;
		}
	}
	
	public SourceItem getModel() {
		return sourceItem;
	}
	
}
